package rsj.service;

import com.rlyy.aibase.utils.base.MapNode;
import common.exec.RsjServiceWarnException;
import common.util.Assert;
import common.util.emailUtils.EmailMessage;
import common.util.emailUtils.EmailUtil;
import org.apache.commons.httpclient.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import rsj.api.account.entity.Organization;
import rsj.api.customer.entity.Customer;
import rsj.context.ApplicationContextUtil;
import rsj.dao.OrgDao;
import rsj.dao.ProductDao;
import rsj.manager.OrgProManager;
import rsj.service.customer.ICustomerService;
import rsj.service.organization.IOrganizationService;
import rsj.table.SYS_ORGPTYPE;

import java.util.Date;

public class OrgProService {

    private static final Logger logger = LoggerFactory.getLogger(OrgProService.class);
    private static final OrgProService instance = new OrgProService();
    public static OrgProService getInstance() {
        return instance;
    }

    private static final OrgProManager orgProManager = OrgProManager.getInstance();
    private static final OrgDao orgDao = OrgDao.getInstance();
    private static final ProductDao prodDao = ProductDao.getInstance();

    public String applyProd(MapNode data, String tid) {
        String Prod_Id = data.getValue("Prod_Id");
        String Org_Id = data.getValue("Org_Id");
        String prodName = prodDao.getById(Prod_Id).getValue("Prod_Name");
        String prodUid = prodDao.getById(Prod_Id).getValue("Prod_Uid");

        // 1.修改表状态
        String orgPId = orgProManager.applyProd(Prod_Id, prodUid, Org_Id, SYS_ORGPTYPE.Orgp_Status_Apply
                , prodName, tid);
        // 2.发送邮件
        sendEmail(Prod_Id, Org_Id);

        return orgPId;
    }

    public static void main(String[] args) {
        sendEmail("RSJ0007", "02");
    }

    private static void sendEmail(String Prod_Id, String Org_Id) {
        IOrganizationService organizationService = ApplicationContextUtil.getBean(IOrganizationService.class);
        ICustomerService customerService = ApplicationContextUtil.getBean(ICustomerService.class);
//        MapNode orgMapNode = orgDao.findOrgById(Org_Id);
        Organization orgMapNode = organizationService.getOrganization(Org_Id);
        Customer customerById = customerService.getCustomerById(orgMapNode.getCustId());
        String custName = customerById.getCustName();

        String toMail = customerById.getCustManbusmail();// 商务负责人邮箱
        String orgManger = customerById.getCustManbus();// 商务负责人

        MapNode prodMapNode = prodDao.getById(Prod_Id);
        String prodName = prodMapNode.getValue("Prod_Name");

        EmailMessage message = new EmailMessage();
        message.setContent(String.format(EmailMessage.emContentTemp
                , orgManger, custName, DateUtil.formatDate(new Date(), "yyyy-MM-dd")
        , prodName));
        message.setReceiveMailAccount(toMail);
        message.setFrom(EmailMessage.emFromTemp);
        message.setSubject(EmailMessage.emSubjectTemp);

        try {
            EmailUtil.sendMail(message);
        } catch (Exception e) {
            throw new RsjServiceWarnException("抱歉，您的申请邮件发送异常，请通过页面下方的联系方式联系我们，本司专员将会处理您的申请。", e);
        }
    }
}
